package com.example.Android_test.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.example.Android_test.R;
import com.example.Android_test.helpers.SOWEngine;
import com.example.Android_test.helpers.UniqViewIDHelper;
import com.example.Android_test.specs.SetOfWordsSpec;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: dns
 * Date: 26.05.13
 * Time: 20:11
 * To change this template use File | Settings | File Templates.
 */
public class SelectSetOfWords extends Activity {
    public static final String TITLE = "SelectSetOfWords";

    private LinearLayout layoutListOfSets;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main_select_setofwords);
        this.setTitle(TITLE);

        layoutListOfSets = (LinearLayout) findViewById(R.id.ssow_setsList);

        fillListOfSets();
    }

    private void fillListOfSets() {
        layoutListOfSets.removeAllViews();

        ArrayList<SetOfWordsSpec> sets = SOWEngine.INSTANCE.getSetOfWordsArrayList();
        for (SetOfWordsSpec currentSet: sets) {

            RelativeLayout relativeLayout = new RelativeLayout(this);
            relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));

            /*RadioButton radioButton = new RadioButton(this);
            radioButton.setId(UniqViewIDHelper.INSTANCE.getUniqueId());
            RelativeLayout.LayoutParams radioButtonLayoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            radioButtonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            radioButton.setLayoutParams(radioButtonLayoutParams);

            relativeLayout.addView(radioButton);*/

            TextView titleTextView = new TextView(this);
            titleTextView.setId(UniqViewIDHelper.INSTANCE.getUniqueId());
            RelativeLayout.LayoutParams titleLayoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            titleTextView.setLayoutParams(titleLayoutParams);
            titleTextView.setText(currentSet.getSowName());

            relativeLayout.addView(titleTextView);


            TextView descriptionTextView = new TextView(this);
            descriptionTextView.setId(UniqViewIDHelper.INSTANCE.getUniqueId());
            RelativeLayout.LayoutParams descriptionLayoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            descriptionLayoutParams.addRule(RelativeLayout.BELOW, titleTextView.getId());
            descriptionTextView.setLayoutParams(descriptionLayoutParams);
            descriptionTextView.setText(currentSet.getSowComment());

            relativeLayout.addView(descriptionTextView);


            Button selectSetButton = new Button(this);
            selectSetButton.setId(UniqViewIDHelper.INSTANCE.getUniqueId());
            RelativeLayout.LayoutParams selectSetLayoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            selectSetLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            selectSetButton.setLayoutParams(selectSetLayoutParams);
            selectSetButton.setText("Select");
            selectSetButton.setTag(currentSet.getSowID());
            selectSetButton.setOnClickListener(new ButtonSelectSetOnClickListener());

            try {
                String alreadySelectedID = SOWEngine.INSTANCE.getSelectedSetOfWord().getSowID();
                if (currentSet.getSowID().equals(alreadySelectedID)) {
                    selectSetButton.setText("selected");
                    selectSetButton.setEnabled(false);
                }
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

            relativeLayout.addView(selectSetButton);


            ImageButton imageButton = new ImageButton(this);

            imageButton.setImageResource(R.drawable.help);
            imageButton.setBackgroundResource(android.R.color.transparent);
            RelativeLayout.LayoutParams imageButtonLayoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            imageButtonLayoutParams.addRule(RelativeLayout.BELOW, selectSetButton.getId());
            imageButtonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            imageButton.setLayoutParams(imageButtonLayoutParams);
            imageButton.setTag(currentSet.getSowID());
            imageButton.setOnClickListener(new ButtonOverviewSetOnClickListener());

            relativeLayout.addView(imageButton);


            View v = new View(this);
            RelativeLayout.LayoutParams vLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 2);
            vLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            v.setLayoutParams(vLayoutParams);
            v.setBackgroundColor(getResources().getColor(R.color.separate_line_in_lists));

            relativeLayout.addView(v);

            layoutListOfSets.addView(relativeLayout);
        }
    }

    private class ButtonOverviewSetOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getBaseContext(), SetOfWordOverview.class);
            Bundle b = new Bundle();
            b.putString("id", (String) view.getTag()); //Your id
            intent.putExtras(b); //Put your id to your next Intent
            startActivity(intent);
        }
    }

    private class ButtonSelectSetOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            String selectedID = (String) view.getTag();
            SOWEngine.INSTANCE.selectSetOfWords(selectedID);

            fillListOfSets();
        }
    }

}